// reservation/order/order-details.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: 0,
        activeIndex: 0,
        details: {},
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.windowInfo = wx.getWindowInfo();
        this.setData({
            statusBarHeight: this.windowInfo.statusBarHeight
        })

        this.id = options.id;
        this.activeId = options.activeId;
        this.getOrder(this.id);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    backTap() {
        wx.navigateBack();
    },

    onTabsClick(event) {
        this.setData({
            activeIndex: event.detail.value
        })

        if(this.data.activeIndex == 0) {
            this.getOrder(this.id);
        } else {
            this.getActivePartList(this.activeId);
        }
    },

    getOrder(id) {
        network.getJson(api.getOrder, {
            id
        }).then(res => {
            let details = res.data;
            let times = details.bookingTimes;
            for (let j = 0; j < times.length; j++) {
                times[j] = formatDate.formatWeek(times[j].startTime, times[j].endTime);
            }
            details.bookingTimes = times;

            details.images = details.siteDO.images.split(',');
            if (details.activeDO) {
                details.images = details.activeDO.images.split(',');
            }
            details.bookingTime = times;
            this.setData({
                details
            })
        })
    },

    getActivePartList(activeId) {
        network.getJson(api.activePartList, {
            activeId
        }).then(res => {
            let list = res.data;
            for (let j = 0; j < list.length; j++) {
                list[j].createTime = formatDate.formatTime(list[j].createTime, 'yyyy.MM.dd HH:mm');
            }
            this.setData({
                list
            })
        })
    },


    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone
        })
    },

    navTap(e) {
        const {
            name,
            lng,
            lat
        } = e.currentTarget.dataset;
        wx.openLocation({
            name,
            latitude: lat,
            longitude: lng
        })
    },
})